﻿// Copyright © 2010 Silvestru Iapascurta.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see license.txt for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FeedDotNet;
using FeedDotNet.Common;

namespace Karoro
{
    public partial class AddFeedDialog : Form
    {
        public event Subscription AddFeed = delegate { };
        private bool extended = false;
        
        public AddFeedDialog()
        {
            InitializeComponent();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (uriBox.Text.Length == 0)
            {
                return;
            }

            if (uriBox.Text.IndexOf("://") < 0)
            {
                uriBox.Text = "http://" + uriBox.Text;
            }

            if (extended)
            {
                if (listBox1.SelectedItems.Count == 0)
                {
                    this.Close();
                    return;
                }
                else if (listBox1.SelectedItems.Count >= 1)
                {
                    List<string> feeds = new List<string>();

                    foreach (ExternalFeedLink link in listBox1.SelectedItems)
                    {
                        feeds.Add(link.Uri);
                    }

                    FeedEventArgs subArg = new FeedEventArgs(feeds);
                    AddFeed(this, subArg);

                    this.Close();
                    return;
                }

            }

            if (!feedDiscoverWorker.IsBusy)
            {
                feedDiscoverWorker.RunWorkerAsync(uriBox.Text);
                uriBox.Enabled = false;
                okButton.Enabled = false;
            }

            statusLabel.Text = "Working. . .";

        }

        private Feed getFeed(string uri)
        {
            Feed feed = FeedReader.Read(uri);

            if (feed != null)
            {   
                return feed;
            }

            return null;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count == 0)
            {
                okButton.Enabled = false;
            }
            else
            {
                okButton.Enabled = true;
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }

            Feed feed = getFeed((string)e.Argument);

            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                if (feed != null)
                {
                    e.Result = feed;
                }
                else
                {
                    feedDiscoverWorker.ReportProgress(50, "Discovering. . .");
                    List<ExternalFeedLink> externalFeedLinks = Explorer.Discover(new Uri((string)e.Argument));
                    e.Result = externalFeedLinks;
                }
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                return;
            }

            statusLabel.ResetText();

            uriBox.Enabled = true;
            okButton.Enabled = true;

            if (e.Result is List<ExternalFeedLink>)
            {
                List<ExternalFeedLink> externalFeedLinks = (List<ExternalFeedLink>)e.Result;

                if (externalFeedLinks.Count == 1)
                {
                    FeedEventArgs subArg = new FeedEventArgs(externalFeedLinks[0].Uri);
                    AddFeed(this, subArg);

                    this.Close();
                    return;
                }
                else if (externalFeedLinks.Count > 1)
                {
                    if (!extended)
                    {
                        extended = true;
                        uriBox.Enabled = false;
                        label1.Text = "Multiple feeds found:";

                        listBox1.Show();
                        this.Height = this.Height + listBox1.Height + listBox1.Margin.Vertical;
                    }

                    listBox1.BeginUpdate();
                    foreach (ExternalFeedLink link in externalFeedLinks)
                    {
                        listBox1.Items.Add(link);
                    }
                    listBox1.EndUpdate();

                    this.listBox1.DataSource = externalFeedLinks;
                    this.listBox1.ValueMember = "Uri";
                    this.listBox1.DisplayMember = "Title";
                }
                else
                {
                    statusLabel.Text = "No feed(s) found.";
                }
            }
            else if (e.Result is Feed)
            {
                FeedEventArgs subArg = new FeedEventArgs(e.Result as Feed);

                AddFeed(this, subArg);                
                this.Close();
                return;
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            feedDiscoverWorker.CancelAsync();
        }

        private void feedDiscoverWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.statusLabel.Text = (string)e.UserState;
        }

    }
}
